package culton.android.sms;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import culton.android.CultOnHome;
import culton.android.R;

public class SMSMoviesActivity extends ListActivity {

	private static String message = null;
	private ProgressDialog m_ProgressDialog = null;
	private Runnable viewMovies;
	private SMSMovieArrayAdapter movieAdapter = null;	 
	ArrayList<String> listMovies;	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        
        //Configuring buttons in Action Bar
        ImageView homeBtn = (ImageView) findViewById(R.id.homeButton);
        homeBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		startActivity (new Intent(SMSMoviesActivity.this, CultOnHome.class));
        	}
        });
        
        ImageView leftArrowBtn = (ImageView) findViewById(R.id.leftArrow);
        leftArrowBtn.setOnClickListener(new OnClickListener() {
        	@Override
        	public void onClick(View arg0) {
        		startActivity (new Intent(SMSMoviesActivity.this, SMSCinemasActivity.class));
        	}
        });

        listMovies = new ArrayList<String>();
        SMSMoviesActivity.message = null;
        
        Intent mIntent = getIntent();
        Bundle mBundle = mIntent.getExtras();
        mIntent.putExtras(mBundle);
        mIntent.putExtra("find", "showtimes");
        
        //Get tracker number from preferences
        SharedPreferences settings =  getSharedPreferences("CultOnPreferences", MODE_PRIVATE);
        String trackerNumber = settings.getString("tracker", "5556");
        mIntent.putExtra("tracker", trackerNumber);

        this.movieAdapter = new SMSMovieArrayAdapter(this, R.layout.movies, new ArrayList<String>(), mIntent);
        setListAdapter(this.movieAdapter);

        viewMovies = new Runnable() {  
            public void run() {
            	retrieveMovies();
            }
        };
	    
        Thread thread = new Thread(null, viewMovies, "MagentoBackground");
        thread.start();
        m_ProgressDialog = ProgressDialog.show(SMSMoviesActivity.this,    
              "Please wait...", "Retrieving data ...", true);
	}
	
	//Get movies from SMS Message
    private void retrieveMovies(){
    	try{
    		int timeout = 0;    		
    		while(SMSMoviesActivity.message == null && timeout < 60) {
    			timeout++;
    			Thread.sleep(500);
    		}
    		
    		if (timeout >= 60) {
    			listMovies.add("Erro ao receber resposta do servidor.");
    			Log.i("erro", "mensagem nao foi recebida");
    			
    		} else {
	    		String message = SMSMoviesActivity.message;
	        	String delimiter = "\\+";
	        	String[] movies = message.split(delimiter);
	        	
	        	for (int i = 0; i < movies.length; i++) {
	        		listMovies.add(movies[i]);        		
	        	}
    		}
    	
    	} catch (Exception e) {
            Log.e("BACKGROUND_PROC", e.getMessage());
    	}
    	runOnUiThread(returnRes);
    }

    private Runnable returnRes = new Runnable() {

        public void run() {
            if(listMovies != null && listMovies.size() > 0){
            	                
                for(int i = 0; i < listMovies.size(); i++)
                	movieAdapter.add(listMovies.get(i));
            }
            m_ProgressDialog.dismiss();
            movieAdapter.notifyDataSetChanged();
        }
    };

    public static void OnMessageReceived(String message) {
    	SMSMoviesActivity.message = message;
    }
}
